var readyScene_layer = cc.Layer.extend({
    sprite_bg:null,
    _winSize:null,
    btnl:null,
    btnr:null,
    thisFontRank:null,
    thisFontNum:null,
    ctor:function () {
        this._super();
        this._winSize = cc.winSize;

        //背景
        this.sprite_bg = new cc.Sprite(res.bg1_jpg);
        this.sprite_bg.attr({
            x: this._winSize.width / 2,
            y: this._winSize.height / 2,
            scale:1
        });
        this.addChild(this.sprite_bg, 0);

        //头部
        var thisTitlePic = new cc.Sprite(res.headPic_png);
        thisTitlePic.attr({
            x:this._winSize.width / 2,
            y:this._winSize.height / 2 + 280,
            scale:1
        });
        this.addChild(thisTitlePic,10,5);

        //白色?
        var thisHumBG = new cc.Sprite(res.numBG_png);
        thisHumBG.attr({
            x:this._winSize.width / 2,
            y:this._winSize.height / 2 + 20,
            scale:1
        });
        this.addChild(thisHumBG,10,5);

        //黑色提示
        var thisBlack = new cc.Sprite(res.black2_png);
        thisBlack.attr({
            x:this._winSize.width / 2,
            y:this._winSize.height / 2 + 90,
            scale:1
        });
        this.addChild(thisBlack,10,5);

        //排名文字
        this.thisFontRank = new cc.LabelTTF("最佳排名：正在读取中...", "Arial", 27);
        this.thisFontRank.enableShadow(cc.color(255, 255, 255),1,1);
        this.thisFontRank.attr({
                x : this._winSize.width / 2,
                y : this._winSize.height / 2 - 65,
                color : cc.color(255, 112, 40)
            });
        this.addChild(this.thisFontRank, 1000);

        //分数文字
        this.thisFontNum = new cc.LabelAtlas("0",res.nums_png,70,100,"0");
        this.thisFontNum.attr({
                x : this._winSize.width / 2 - this.thisFontNum.width / 2,
                y : this._winSize.height / 2 - 45,
            });

        this.addChild(this.thisFontNum, 10);

        //按钮l
        var playAgainNormal = new cc.Sprite(res.btnl_png);
            playAgainNormal.attr({
            scale:1
        });
        var playAgainSelected = new cc.Sprite(res.btnl_png);
            playAgainSelected.attr({
            scale:0.95
        });
        var playAgainDisabled = new cc.Sprite(res.btnl_png);
            playAgainDisabled.attr({
            scale:0.95
        });
        var playAgain = new cc.MenuItemSprite(
            playAgainNormal,
            playAgainSelected,
            playAgainDisabled,
            this.onDesc,this);

        this.btnl = new cc.Menu(playAgain);
        this.btnl.attr({
            x:this._winSize.width / 2 - thisTitlePic.width / 3.2,
            y:this._winSize.height - playAgainNormal.height / 2,
            scale:1
        });
        this.addChild(this.btnl,10);

        //按钮r
        var playAgainNormal1 = new cc.Sprite(res.btnr_png);
            playAgainNormal1.attr({
            scale:1
        });
        var playAgainSelected1 = new cc.Sprite(res.btnr_png);
            playAgainSelected1.attr({
            scale:0.95
        });
        var playAgainDisabled1 = new cc.Sprite(res.btnr_png);
            playAgainDisabled1.attr({
            scale:0.95
        });
        var playAgain1 = new cc.MenuItemSprite(
            playAgainNormal1,
            playAgainSelected1,
            playAgainDisabled1,
            this.onRank,this);

        this.btnr = new cc.Menu(playAgain1);
        this.btnr.attr({
            x:this._winSize.width / 2 + thisTitlePic.width / 3.2,
            y:this._winSize.height - playAgainNormal1.height / 2,
            scale:1
        });
        this.addChild(this.btnr,10);

        //读取接口加载游戏信息
        this.getGameInfo();

        return true;
    },
    getGameInfo:function(){
        var thisInfo;
        var that = this;
        var xhr = cc.loader.getXMLHttpRequest();
        var args = "openid="+userData.openid+"&id="+userData.id;

        // xhr.open("POST", "/MicroRankGameTel/GetGameStatus");
        // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset\x3dUTF-8");
        // xhr.send(args);
        // xhr.onreadystatechange = function () {
        //     if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
        //         thisInfo = JSON.parse(xhr.responseText);
        //         that.setUI(thisInfo);
        //     }
        // };

        thisInfo = {
           //0未开始 1进行中 2已结束
           status:1,
           beginDate: "2015/10/29 13:54",
           score:934,
           rank:2
        }
        this.setUI(thisInfo);
    },
    setUI:function(resObj){
        this.thisFontRank.setString("最佳排名：第"+resObj.rank+"名");
        this.thisFontRank.attr({
            x : this._winSize.width / 2 ,
        });
        this.thisFontNum.setString(resObj.score);
        this.thisFontNum.attr({
            x : this._winSize.width / 2 - this.thisFontNum.width / 2,
        });
        if(resObj.status == 0){

            //活动未开始
            var thisTips = new cc.Sprite(res.gameUnstart_png);
                thisTips.attr({
                    x:this._winSize.width / 2,
                    y:this._winSize.height / 2 - 180,
                    scale:1
                });
            this.addChild(thisTips,10);

            //开始时间
            var thisFontTime = new cc.LabelTTF("开始时间：" + resObj.beginDate, "Arial", 27);
                thisFontTime.enableShadow(cc.color(255, 255, 255),1,1);
                thisFontTime.attr({
                    x:this._winSize.width / 2,
                    y:this._winSize.height / 2 - 225,
                    color : cc.color(0, 0, 0)
                });
            this.addChild(thisFontTime, 1000);

        }else if(resObj.status == 1){

            //开始游戏
            var playBeginNormal = new cc.Sprite(res.obj_png);
                playBeginNormal.attr({
                scale:1
            });
            var playBeginSelected = new cc.Sprite(res.obj_png);
                playBeginSelected.attr({
                scale:0.95
            });
            var playBeginDisabled = new cc.Sprite(res.obj_png);
                playBeginDisabled.attr({
                scale:0.95
            });
            var playBegin = new cc.MenuItemSprite(
                playBeginNormal,
                playBeginSelected,
                playBeginDisabled,
                this.onPlayBegin,this);
                this.playBegin = new cc.Menu(playBegin);
                this.playBegin.attr({
                    x:this._winSize.width / 2,
                    y:this._winSize.height / 2 - 230,
                    scale:1
                });
            this.addChild(this.playBegin,10);

        }else{

            //活动已结束
            var thisTips = new cc.Sprite(res.gameEnd_png);
                thisTips.attr({
                    x:this._winSize.width / 2,
                    y:this._winSize.height / 2 - 180,
                    scale:1
                });
            this.addChild(thisTips,10);

            //查看结果
            var btnInfoS1 = new cc.Sprite(res.btnInfo_png);
                btnInfoS1.attr({
                scale:1
            });
            var btnInfoS2 = new cc.Sprite(res.btnInfo_png);
                btnInfoS2.attr({
                scale:0.95
            });
            var btnInfoS3 = new cc.Sprite(res.btnInfo_png);
                btnInfoS3.attr({
                scale:0.95
            });
            var btnInfoSBtn = new cc.MenuItemSprite(
                btnInfoS1,
                btnInfoS2,
                btnInfoS3,
                this.onRes,this);

            var btnInfo_btn = new cc.Menu(btnInfoSBtn);
                btnInfo_btn.attr({
                x:this._winSize.width / 2,
                y:this._winSize.height / 2 - 220,
                scale:1
            });
            this.addChild(btnInfo_btn,10);

        }
    },
    onPlayBegin:function(){
        cc.director.runScene(new cc.TransitionFade(0.2, new playScene()));
    },
    onDesc:function(){
        indexDesc();
    },
    onRank:function(){
        indexRank();
    },
    onRes:function(){
        indexRes();
    }
});

var endScene_layer = cc.Layer.extend({
    _winSize:null,
    btnOnAgain:null,
    btnOnShare:null,
    btnOnDesc:null,
    btnOnRank:null,
    thisFontNum:null,
    thisFontRank:null,
    ctor:function () {
        this._super();
        this._winSize = cc.winSize;

        //背景
        var sprite_bg = new cc.Sprite(res.bg1_jpg);
        sprite_bg.attr({
            x: this._winSize.width / 2,
            y: this._winSize.height / 2,
            scale:1
        });
        this.addChild(sprite_bg, 0);

        //头部
        var thisTitlePic = new cc.Sprite(res.headPic_png);
        thisTitlePic.attr({
            x:this._winSize.width / 2,
            y:this._winSize.height / 2 + 280,
            scale:1
        });
        this.addChild(thisTitlePic,10,5);

        //白色?
        var thisHumBG = new cc.Sprite(res.numBG_png);
        thisHumBG.attr({
            x:this._winSize.width / 2,
            y:this._winSize.height / 2 + 20,
            scale:1
        });
        this.addChild(thisHumBG,10,5);

        //黑色提示
        var thisBlack = new cc.Sprite(res.black1_png);
        thisBlack.attr({
            x:this._winSize.width / 2,
            y:this._winSize.height / 2 + 90,
            scale:1
        });
        this.addChild(thisBlack,10,5);

        //分数文字
        this.thisFontNum = new cc.LabelAtlas(Zongfen,res.nums_png,70,100,"0");
        this.thisFontNum.attr({
            x : this._winSize.width / 2 - this.thisFontNum.width / 2,
            y : this._winSize.height / 2 - 45,
        });
        this.addChild(this.thisFontNum, 10);

        //排名文字
        this.thisFontRank = new cc.LabelTTF("最佳排名：正在读取中...", "Arial", 27);
        this.thisFontRank.enableShadow(cc.color(255, 255, 255),1,1);
        this.thisFontRank.attr({
            x : this._winSize.width / 2,
            y : this._winSize.height / 2 - 65,
            color : cc.color(255, 112, 40)
        });
        this.addChild(this.thisFontRank, 1000);

        //按钮l
        var playAgainNormal = new cc.Sprite(res.btnl_png);
            playAgainNormal.attr({
            scale:1
        });
        var playAgainSelected = new cc.Sprite(res.btnl_png);
            playAgainSelected.attr({
            scale:0.95
        });
        var playAgainDisabled = new cc.Sprite(res.btnl_png);
            playAgainDisabled.attr({
            scale:0.95
        });
        var playAgain = new cc.MenuItemSprite(
            playAgainNormal,
            playAgainSelected,
            playAgainDisabled,
            this.onDesc,this);

        this.btnOnDesc = new cc.Menu(playAgain);
        this.btnOnDesc.attr({
            x:this._winSize.width / 2 - thisTitlePic.width / 3.2,
            y:this._winSize.height - playAgainNormal.height / 2,
            scale:1
        });
        this.addChild(this.btnOnDesc,10);

        //按钮r
        var playAgainNormal1 = new cc.Sprite(res.btnr_png);
            playAgainNormal1.attr({
            scale:1
        });
        var playAgainSelected1 = new cc.Sprite(res.btnr_png);
            playAgainSelected1.attr({
            scale:0.95
        });
        var playAgainDisabled1 = new cc.Sprite(res.btnr_png);
            playAgainDisabled1.attr({
            scale:0.95
        });
        var playAgain1 = new cc.MenuItemSprite(
            playAgainNormal1,
            playAgainSelected1,
            playAgainDisabled1,
            this.onRank,this);

        this.btnOnRank = new cc.Menu(playAgain1);
        this.btnOnRank.attr({
            x:this._winSize.width / 2 + thisTitlePic.width / 3.2,
            y:this._winSize.height - playAgainNormal1.height / 2,
            scale:1
        });
        this.addChild(this.btnOnRank,10);

        //保存分数
        this.SavePoint(Zongfen);

        return true;
    },
    onPlayAgain:function(){
        cc.director.runScene(new cc.TransitionFade(0.2, new playScene()));
    },
    onShare:function(){
        indexShare();
    },
    onRank:function(){
        indexRank();
    },
    onDesc:function(){
        indexDesc();
    },
    SavePoint: function(zongfen){
        var that = this;
        var xhr = cc.loader.getXMLHttpRequest();

        // xhr.open("POST", "/MicroRankGameTel/AddRecord/"+userData.id);
        // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset\x3dUTF-8");
        // xhr.onreadystatechange = function () {
        //     if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
        //         var thisInfo = JSON.parse(xhr.responseText);
        //         console.log("保存成功");
        //         that.getPoint();
        //     }
        // };
        // var args = "openid="+userData.openid+"&pid="+userData.pid+"&score="+zongfen;
        // xhr.send(args);

        // that.getPoint();
    },
    getPoint:function(){
        var thisInfo;
        var that = this;
        var xhr = cc.loader.getXMLHttpRequest();
        var args = "openid="+userData.openid+"&id="+userData.id;

        // xhr.open("POST", "/MicroRankGameTel/GetGameStatus");
        // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset\x3dUTF-8");
        // xhr.send(args);
        // xhr.onreadystatechange = function () {
        //     if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
        //         thisInfo = JSON.parse(xhr.responseText);
        //         that.showBtns(thisInfo);
        //     }
        // };

        thisInfo = {
           //0未开始 1进行中 2已结束
           status:1,
           beginDate: "2015/10/29 13:54",
           score:4444,
           rank:44
        }
        this.showBtns(thisInfo);
    },
    showBtns:function(resObj){
        this.thisFontRank.setString("最佳排名：第"+resObj.rank+"名");
        this.thisFontRank.attr({
            x : this._winSize.width / 2 ,
        });
        //在玩一次
        var playBeginNormal2 = new cc.Sprite(res.aginBtn0_png);
            playBeginNormal2.attr({
            scale:1
        });
        var playBeginSelected2 = new cc.Sprite(res.aginBtn0_png);
            playBeginSelected2.attr({
            scale:0.95
        });
        var playBeginDisabled2 = new cc.Sprite(res.aginBtn0_png);
            playBeginDisabled2.attr({
            scale:0.95
        });
        var playBegin = new cc.MenuItemSprite(
            playBeginNormal2,
            playBeginSelected2,
            playBeginDisabled2,
            this.onPlayAgain,this);

            this.btnOnAgain = new cc.Menu(playBegin);
            this.btnOnAgain.attr({
                x:this._winSize.width / 2 - 140,
                y:this._winSize.height / 2 - 180,
                scale:1
            });
        this.addChild(this.btnOnAgain,10);

        //告诉朋友
        var playBeginNormal3 = new cc.Sprite(res.aginBtn1_png);
            playBeginNormal3.attr({
            scale:1
        });
        var playBeginSelected3 = new cc.Sprite(res.aginBtn1_png);
            playBeginSelected3.attr({
            scale:0.95
        });
        var playBeginDisabled3 = new cc.Sprite(res.aginBtn1_png);
            playBeginDisabled3.attr({
            scale:0.95
        });
        var playShare = new cc.MenuItemSprite(
            playBeginNormal3,
            playBeginSelected3,
            playBeginDisabled3,
            this.onShare,this);

        this.btnOnShare = new cc.Menu(playShare);
        this.btnOnShare.attr({
                x:this._winSize.width / 2 + 140,
                y:this._winSize.height / 2 - 180,
            scale:1
        });

        this.addChild(this.btnOnShare,10);
    }
});

var playScene_layer = cc.Layer.extend({
    _winSize:null,
    gameBG:null,
    numBG:null,
    blackText:null,
    fontNum:null,
    fontTime:null,
    fontTimeIcon:null,
    timeSec:null,
    upDateCount:null,
    newObjNum:null,
    ctor:function () {
        this._super();
        this._winSize = cc.winSize;
        Zongfen = 0;

        //背景
        this.gameBG = new cc.Sprite(res.bg2_jpg);
        this.gameBG.attr({
            x: this._winSize.width / 2,
            y: this._winSize.height / 2,
            scale:1
        });
        this.addChild(this.gameBG, 0);

        //白色
        this.numBG = new cc.Sprite(res.numBG_png);
        this.numBG.attr({
            x:this._winSize.width / 2,
            y:this._winSize.height - this.numBG.height / 2 - 10,
            scale:1
        });
        this.addChild(this.numBG,5);

        //黑色提示
        this.blackText = new cc.Sprite(res.black1_png);
        this.blackText.attr({
            x:this._winSize.width / 2,
            y:this._winSize.height - this.blackText.height / 2 - 25,
            scale:1
        });
        this.addChild(this.blackText,6);

        //分数文字
        this.fontNum = new cc.LabelAtlas("0",res.nums_png,70,100,"0");
        this.fontNum.attr({
            x : this._winSize.width / 2 - this.fontNum.width / 2,
            y : this._winSize.height - 70 - 130,
        });
        this.addChild(this.fontNum,6);

        //时间图标
        this.fontTimeIcon = new cc.Sprite(res.oclock_png);
        this.fontTimeIcon.attr({
            x:this._winSize.width / 2 - 30,
            y : this._winSize.height - 200 - 60,
            scale:1
        });
        this.addChild(this.fontTimeIcon,6);

        //时间text
        this.fontTime = new cc.LabelTTF("0秒", "幼圆", 27);
            this.fontTime.enableShadow(cc.color(255, 255, 255),1,1);
            this.fontTime.attr({
                x : this._winSize.width / 2 + 20,
                y : this._winSize.height - 200 - 60,
                color : cc.color(255, 255, 255)
            });
        this.addChild(this.fontTime, 6);

        //初始化时间统计和音乐
        this.timeSec = 0;
        cc.audioEngine.playMusic(res.bg_mp3, true);

        //精准计时
        this.newObjNum = 0;
        this.objArr = new Array();
        this.scheduleNew(function(){
            this.timeSec ++ ;

            this.updateUI(this.timeSec);
            this.updateCount(this.timeSec);
        },0.1);

        return true;
    },
    deletePoint:function(obj){
        this.removeChild(obj);
    },
    scheduleNew:function(callback,interval){
        var then = Date.now();
        interval = interval * 1000;
        this.schedule(function(){
            var now = Date.now();
            var delta = now - then;
            if(delta > interval){
                then = now - (delta % interval);
                callback.call(this);
            }
        }.bind(this),0);
    },
    updateCount:function(time){
        this.newObjNum ++ ;
        if(this.newObjNum == 2){
            this.newObjNum = 0;
            var _randomX= parseInt(Math.random()*this._winSize.width);
            var _randomY= parseInt(Math.random()*this._winSize.height);
            var _randomType = Math.floor(Math.random()*3) + 1;
            var _randomScale;
                switch(_randomType){
                    case 1:
                        _randomScale = 0.6;
                        break;
                    case 2:
                        _randomScale = 0.45;
                        break;
                    case 3:
                        _randomScale = 0.3;
                        break;
                }
            var thisClickObj = new clickSprite(res.obj_png,_randomScale,_randomType);
                thisClickObj.attr({
                    x:_randomX,
                    y:_randomY,
                    scale:_randomScale
                });
            this.addChild(thisClickObj,10);
        }
    },
    updateUI:function(time){
        this.fontTime.setString(20 - parseInt(time / 10) +'秒');
        if(time == 200){
            cc.audioEngine.stopMusic();
            cc.director.runScene(new cc.TransitionFade(0.2, new endScene()));
        }
    },
    changePointFont:function(type){
        switch(type){
            case 1:
                Zongfen += 1;
                break;
            case 2:
                Zongfen += 2;
                break;
            case 3:
                Zongfen += 3;
                break;
        }
        this.fontNum.setString(Zongfen);
        this.fontNum.attr({
            x : this._winSize.width / 2 - this.fontNum.width / 2
        });
    },
    plusPoint:function(x,y,type){
        var newPointPic;
            switch(type){
                case 1:
                    newPointPic = new cc.Sprite(res.p1_png);
                    break;
                case 2:
                    newPointPic = new cc.Sprite(res.p2_png);
                    break;
                case 3:
                    newPointPic = new cc.Sprite(res.p3_png);
                    break;
            }
            newPointPic.attr({
                x:x,
                y:y
            });
            newPointPic.runAction(
                cc.repeatForever(
                    cc.spawn(
                        cc.fadeOut(0.5),
                        cc.moveTo(0.5,cc.p(x,y+50)).easing(cc.easeOut(2)),
                        cc.sequence(
                            cc.delayTime(0.5),
                            cc.callFunc(this.deletePoint,this)
                        )
                    )
                )
            );
        this.addChild(newPointPic,5);
        this.changePointFont(type);
    }
});

